package com.andy.blog.admin.controller;

import com.andy.blog.admin.service.SysUserBusService;
import com.andy.blog.common.annotation.Log;
import com.andy.blog.common.enums.BusinessTypeEnum;
import com.andy.blog.common.reponse.GeneralResponseResult;
import com.andy.blog.dal.vo.SysUserVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 个人信息
 * Created by andy on 2023-04-05 11:38
 */
@RestController
@RequestMapping("/system/user/profile")
public class SysProfileController {

    @Resource
    private SysUserBusService sysUserBusService;


    /**
     * 个人信息
     */
    @GetMapping("getUserInfo")
    public GeneralResponseResult<Map<String, Object>> profile() {
        return GeneralResponseResult.success(sysUserBusService.profile());
    }

    /**
     * 修改用户
     */
    @Log(title = "个人信息", businessType = BusinessTypeEnum.UPDATE)
    @PutMapping
    public GeneralResponseResult<Void> updateProfile(@RequestBody SysUserVO sysUserVO) {
        sysUserBusService.updateProfile(sysUserVO);
        return GeneralResponseResult.success();
    }

    /**
     * 重置密码
     */
    @Log(title = "个人信息", businessType = BusinessTypeEnum.UPDATE)
    @PutMapping("/updatePwd")
    public GeneralResponseResult<String> updatePwd(String oldPassword, String newPassword) {
        sysUserBusService.updatePwd(oldPassword, newPassword);
        return GeneralResponseResult.success();
    }

    /**
     * 头像上传
     */
    @Log(title = "用户头像", businessType = BusinessTypeEnum.UPDATE)
    @PostMapping("/avatar")
    public GeneralResponseResult<Map<String, String>> avatar(@RequestParam("avatarfile") MultipartFile file) {
        return GeneralResponseResult.success(sysUserBusService.avatar(file));
    }
}
